package org.extframework.ld.http.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.extframework.ld.bean.Power;
import org.extframework.ld.control.ExtJsonControl;
import org.extframework.ld.http.abstr.AbstractEncryptionWebRequest;
import org.extframework.ld.http.abstr.AbstractEncryptionWebResponse;
import org.extframework.ld.http.abstr.AbstractEncryptionWebServlet;
import org.extframework.ld.service.PowerManager;
import org.extframework.ld.util.StringUtils;
import org.extframework.ld.xml.base.ComboBoxData;
import org.extframework.ld.xml.base.ComboBoxDataDetail;

public class PowerServlet extends AbstractEncryptionWebServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private PowerManager manager;

	protected void init(
			AbstractEncryptionWebRequest paramAbstractEncryptionWebRequest,
			AbstractEncryptionWebResponse paramAbstractEncryptionWebResponse)
			throws ServletException, IOException {
		manager = getBean(PowerManager.class);
	}

	public void index(AbstractEncryptionWebRequest paramHttpServletRequest,
			AbstractEncryptionWebResponse paramHttpServletResponse)
			throws ServletException, IOException {
		
	}
	
	//右键菜单
	public void rPowers(AbstractEncryptionWebRequest request,
			AbstractEncryptionWebResponse response) throws IOException{
		
		String role = request.getParameter("role");
		String power = request.getParameter("power");
		String resp;
		
		if(StringUtils.isEmpty(role)){//TODO 取消全部权限
			resp = manager.getRPowerAllToJson( Long.parseLong(power),null );
			//resp = manager.rPowers(null, Long.parseLong(power));
		}else{
			resp = manager.getRPowerAllToJson( Long.parseLong(power),Long.parseLong(role) );
			//resp = manager.rPowers(Long.parseLong(role), Long.parseLong(power));
		}
		
		if(resp == null){
			response.printJson("{success:false,responseText:'该页面 没有右键菜单'}");
		}else{
			response.printJson("{success:true,responseText:["+resp+"]}");
		}
		
	}
	
	public void storePR(AbstractEncryptionWebRequest request,
			AbstractEncryptionWebResponse response) throws IOException{
		String role = request.getParameter("role");
		String powers = request.getParameter("powers");
		String rPowers = request.getParameter("rPowers");
		try{
			if( !StringUtils.isEmpty(role)){
				manager.storeRP(Long.parseLong(role),powers,rPowers);
			}
			
		}catch( NumberFormatException e){
			response.printJson("{success:false,responseText:'系统页面校验失败'}");
		}
		response.printJson("{success:true}");
	}
	
	public void getPowerAll(AbstractEncryptionWebRequest request,
			AbstractEncryptionWebResponse response) throws IOException{
		String powerjson = manager.getPowerAllToJson();
		
		response.printJson("{success:true,responseText:["+powerjson+"]}");
	}
	
	public void getPowerIdsByRole(AbstractEncryptionWebRequest request,
			AbstractEncryptionWebResponse response) throws IOException{
		String role = request.getParameter("role");
		
		Long roleId = null;
		try {
			roleId = Long.parseLong(role);
		} catch ( NumberFormatException e) {
			e.printStackTrace();
			response.printJson("{success:false,responseText:'系统页面校验失败'}");
			return;
		}
		
		Set<Power> powers = manager.getPowerIdsByRole(roleId);
		
		if(powers.size() == 0){
			response.printJson("{success:true,responseText:[]}");
			return;
		}
		
		StringBuffer buffer = new StringBuffer();
		for( Power power : powers ){
			buffer.append("["+power.getId()+"],");
		}
		
		String powerIds = buffer.toString().substring(0, buffer.length()-1);
		response.printJson("{success:true,responseText:["+powerIds+"]}");
	}
	
	public void getRoleAll(AbstractEncryptionWebRequest request,
			AbstractEncryptionWebResponse response) throws IOException{
		
		String limitStr = request.getParameter("limit");
		String queryStr = request.getParameter("query");
		String startStr = request.getParameter("start");
		
		int start = 0;
		int end = 6;
		try {
			start = Integer.parseInt(startStr);
			end = Integer.parseInt(limitStr);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		Map<String, List<Object[]>> map = this.manager.getRoleAll(start, end ,queryStr);

		ComboBoxData boxData = new ComboBoxData();
		for (Object[] objs : map.entrySet().iterator().next().getValue()) {
			ComboBoxDataDetail boxDataDetail = new ComboBoxDataDetail();
			boxDataDetail.setId(objs[0].toString());
			boxDataDetail.setValue(objs[1].toString());
			boxData.getDetails().add(boxDataDetail);
		}
		boxData.setTotalNum(Integer.parseInt(map.entrySet().iterator().next().getKey()));
		response.printJson(ExtJsonControl.toJson(boxData));
	}

}
